package com.example.jdbc.datasource;
import java.sql.SQLException;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
@Configuration
@PropertySource(value = {"classpath:application-dev.properties",
"file:/E:\\Users\\zxj\\work04\\springboot\\src\\main\\resources\\application-dev.properties"},
ignoreResourceNotFound = true)
@ConfigurationProperties(prefix = "")

public class DataSourceConfig {
 private String url;
 private String username;
 private String password;
 private String drivercLassName;
 private int initialSize;
 private int minIdle;
 private int maxActive;
 private int maxWait;
 private int timeBetweenEvictionRunsMillis;
 private int minEvictableIdleTimeMillis;
 private String validationQuery;
 private boolean testWhileIdle;
 private boolean testOnBorrow;
 private boolean testOnReturn;
 private boolean poolPreparedStatements;
 private int maxPoolPreparedStatementPerConnectionSize;
 private String filters;
 private String connectionProperties;

 @Bean
 public DataSource dataSource(){
     DruidDataSource dataSource=new DruidDataSource();
     dataSource.setUrl(url);
     dataSource.setUsername(username);
     dataSource.setPassword(password);
     dataSource.setDriverClassName(drivercLassName);
     //Configuration
     dataSource.setInitialSize(initialSize);
     dataSource.setMinIdle(minIdle);
     dataSource.setMaxActive(maxActive);
     dataSource.setMaxWait(maxWait);
     dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
     dataSource.setTestWhileIdle(testWhileIdle);
     dataSource.setTestOnBorrow(testOnBorrow);
     dataSource.setTestOnReturn(testOnReturn);
     dataSource.setPoolPreparedStatements(poolPreparedStatements);
     dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
     try {
         dataSource.setFilters(filters);
     } catch (SQLException e) {
         System.err.println("druid configuration initialization filter: " + e);
     }
     dataSource.setConnectionProperties(connectionProperties);
     return dataSource;
 }
    public void setUrl(String url) {
        this.url = url;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setDrivercLassName(String drivercLassName) {
        this.drivercLassName = drivercLassName;
    }

    public void setInitialSize(int initialSize) {
        this.initialSize = initialSize;
    }

    public void setMinIdle(int minIdle) {
        this.minIdle = minIdle;
    }

    public void setMaxActive(int maxActive) {
        this.maxActive = maxActive;
    }

    public void setMaxWait(int maxWait) {
        this.maxWait = maxWait;
    }

    public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    }

    public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
        this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    }

    public void setValidationQuery(String validationQuery) {
        this.validationQuery = validationQuery;
    }

    public void setTestWhileIdle(boolean testWhileIdle) {
        this.testWhileIdle = testWhileIdle;
    }



    public void setPoolPreparedStatements(boolean poolPreparedStatements) {
        this.poolPreparedStatements = poolPreparedStatements;
    }

    public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
        this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
    }


    public void setConnectionProperties(String connectionProperties) {
        this.connectionProperties = connectionProperties;
    }

    public String getUrl() {

        return url;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getDrivercLassName() {
        return drivercLassName;
    }

    public int getInitialSize() {
        return initialSize;
    }

    public int getMinIdle() {
        return minIdle;
    }

    public int getMaxActive() {
        return maxActive;
    }

    public int getMaxWait() {
        return maxWait;
    }

    public int getTimeBetweenEvictionRunsMillis() {
        return timeBetweenEvictionRunsMillis;
    }

    public int getMinEvictableIdleTimeMillis() {
        return minEvictableIdleTimeMillis;
    }

    public String getValidationQuery() {
        return validationQuery;
    }

    public boolean isTestWhileIdle() {
        return testWhileIdle;
    }



    public boolean isPoolPreparedStatements() {
        return poolPreparedStatements;
    }

    public int getMaxPoolPreparedStatementPerConnectionSize() {
        return maxPoolPreparedStatementPerConnectionSize;
    }

    public boolean isTestOnBorrow() {
        return testOnBorrow;
    }

    public boolean isTestOnReturn() {
        return testOnReturn;
    }

    public String getFilters() {
        return filters;
    }

    public String getConnectionProperties() {
        return connectionProperties;
    }
}
